﻿Imports DATA
Public Class oFm_funcionario : Inherits oFmBase

    Private WithEvents oFun As New funcionario_data
    Private _Codigo As String = ""

    Private Sub oFm_libro_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Me.Dispose()
    End Sub

    Private Sub oFm_libro_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.TituloFormulario = "Crear/Modificar funcionario"
        EstructuraGrilla()
        CargaGrillaFuncionarios()
        CargaGrupo()
    End Sub

    Sub EstructuraGrilla()
        With oGr_datos
            .Cols_P = 3
            .Rows_P = 0
            .MaquillaGrilla()
            .ReadOnly = True
            .ColumnTextHeader(0, 300) = "Nombre"
            .ColumnTextHeader(1, 95) = "Grado"
            .ColumnTextHeader(2, 210) = "Grupo"
            For i As Integer = 0 To .ColumnCount - 1
                .Columns(i).SortMode = DataGridViewColumnSortMode.Automatic
            Next
        End With
    End Sub

    Sub CargaGrupo()
        With oCb_grupo
            .DataSource = oFun.ObtenerGrupos
            .DisplayMember = "GRUPO_NOMBRE"
            .ValueMember = "GRUPO_ID"
            .SelectedValue = -1
        End With
    End Sub

    Sub CargaGrillaFuncionarios()
        oGr_datos.Rows.Clear()
        Dim dt As DataTable = oFun.GetData
        If Not IsNothing(dt) Then
            If dt.Rows.Count > 0 Then
                With oGr_datos
                    oGr_datos.Rows_P = dt.Rows.Count
                    For i As Integer = 0 To dt.Rows.Count - 1
                        .TagMatrix(i, 0) = dt.Rows(i).Item("FUN_RUT")
                        .TextMatrix(i, 0) = dt.Rows(i).Item("FUN_NOMBRE")
                        .TextMatrix(i, 1) = dt.Rows(i).Item("FUN_GRADO")
                        .TextMatrix(i, 2) = dt.Rows(i).Item("GRUPO_NOMBRE")
                    Next
                End With
            End If
        End If
    End Sub

    Sub CargaGrillaFuncionarioxNombre(ByVal rut As String)
        oGr_datos.Rows.Clear()
        Dim dt As DataTable = oFun.msoBuscar(rut)
        If Not IsNothing(dt) Then
            If dt.Rows.Count > 0 Then
                With oGr_datos
                    oGr_datos.Rows_P = dt.Rows.Count
                    For i As Integer = 0 To dt.Rows.Count - 1
                        oTx_rut.Text = Mid(dt.Rows(i).Item("FUN_RUT"), 1, 8)
                        oTx_nombre.Text = dt.Rows(i).Item("FUN_NOMBRE")
                        oTx_peso.Text = dt.Rows(i).Item("FUN_PESO")
                        oTx_edad.Text = dt.Rows(i).Item("FUN_EDAD")
                        oTx_estatura.Text = dt.Rows(i).Item("FUN_ESTATURA")
                        oTx_imc.Text = dt.Rows(i).Item("FUN_IMC")
                        'oCb_unidad.Text = dt.Rows(i).Item("FUN_UNIDAD")
                        oCb_grado.Text = dt.Rows(i).Item("FUN_GRADO")
                        oCb_grupo.SelectedValue = dt.Rows(i).Item("GRUPO_ID")
                    Next
                End With
            End If
        End If
    End Sub

    Sub CargaGrillaFuncionarioxNombreLike(ByVal nombre As String)
        oGr_datos.Rows.Clear()
        Dim dt As DataTable = oFun.msoBuscarLike(nombre)
        If Not IsNothing(dt) Then
            If dt.Rows.Count > 0 Then
                With oGr_datos
                    oGr_datos.Rows_P = dt.Rows.Count
                    For i As Integer = 0 To dt.Rows.Count - 1
                        .TagMatrix(i, 0) = dt.Rows(i).Item("FUN_RUT")
                        oTx_nombre.Text = dt.Rows(i).Item("FUN_NOMBRE")
                        'oCb_unidad.Text = dt.Rows(i).Item("FUN_UNIDAD")
                        oCb_grado.Text = dt.Rows(i).Item("FUN_GRADO")
                        oCb_grupo.SelectedValue = dt.Rows(i).Item("GRUPO_ID")
                    Next
                End With
            End If
        End If
    End Sub

    Sub Limpiar()
        oTx_rut.Text = "0"
        oTx_nombre.Text = ""
        oTx_peso.Text = "0"
        oTx_edad.Text = "0"
        oTx_estatura.Text = "0"
        oCb_grado.SelectedIndex = -1
        'oCb_unidad.SelectedIndex = -1
        oCb_grupo.SelectedValue = -1
        CargaGrillaFuncionarios()
    End Sub

    Private Sub oPb_guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles oPb_guardar.Click
        If Val(oTx_rut.Text) = 0 Then MsgBox("Debe ingresar el RUT del funcionario", MsgBoxStyle.Exclamation, Me.Text) : Exit Sub
        If oTx_nombre.Text = "" Then MsgBox("Debe ingresar el nombre del funcionario", MsgBoxStyle.Exclamation, Me.Text) : Exit Sub
        'If oCb_unidad.SelectedIndex = -1 Then MsgBox("Debe ingresar la unidad", MsgBoxStyle.Exclamation, Me.Text) : Exit Sub
        If oCb_grado.SelectedIndex = -1 Then MsgBox("Debe ingresar el grado", MsgBoxStyle.Exclamation, Me.Text) : Exit Sub
        If oCb_grupo.SelectedIndex = -1 Then MsgBox("Debe ingresar el grupo al pertence", MsgBoxStyle.Exclamation, Me.Text) : Exit Sub

        oFun.minGuarda(oTx_rut.Text & "-" & oTx_dv.Text, oTx_nombre.Text, "", oCb_grado.Text, oCb_grupo.SelectedValue, oTx_edad.Text, oTx_peso.Text, oTx_estatura.Text, oTx_imc.Text)
    End Sub

    Private Sub oFun_ErrorServer(ByVal Descripcion As String) Handles oFun.ErrorServer
        MsgBox(Descripcion, MsgBoxStyle.Critical, Me.Text)
    End Sub

    Private Sub oFun_GuardaCorrecto() Handles oFun.GuardaCorrecto
        Limpiar()
        CargaGrillaFuncionarios()
    End Sub

    Private Sub oPb_Limpiar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles oPb_Limpiar.Click
        Limpiar()
    End Sub

    Private Sub oPb_Salir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles oPb_Salir.Click
        Me.Close()
    End Sub


    Private Sub oPb_Eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles oPb_Eliminar.Click
        If MessageBox.Show("¿Esta seguro de eliminar el Libro?", Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            oFun.mdnElimina(oTx_nombre.Text)
        End If
    End Sub

    Private Sub oGr_datos_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles oGr_datos.CellDoubleClick
        If oGr_datos.Rows.Count > 0 Then
            _Codigo = oGr_datos.TagMatrix(oGr_datos.Row, 0)
            CargaGrillaFuncionarioxNombre(_Codigo)
            CargaGrillaFuncionarios()
        End If
    End Sub

    Private Sub oTx_filtroNombre_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles oTx_filtroNombre.TextChanged
        If oTx_filtroNombre.Text <> "" Then
            CargaGrillaFuncionarioxNombreLike(oTx_filtroNombre.Text)
        Else
            CargaGrillaFuncionarios()
        End If
    End Sub

    Private Sub oTx_rut_TextChanged(sender As Object, e As EventArgs) Handles oTx_rut.TextChanged
        oTx_dv.Text = oTx_rut.GetDv
    End Sub

    Function CalcularIMC(peso As Integer, estatura As Integer) As Double
        If peso = 0 Or estatura = 0 Then Return 0
        Return Math.Round(peso / Math.Pow((estatura * 0.01), 2), 2)
    End Function


    Private Sub oTx_peso_TextChanged(sender As Object, e As EventArgs) Handles oTx_peso.TextChanged
        oTx_imc.Text = CalcularIMC(Val(oTx_peso.Text), Val(oTx_estatura.Text))
    End Sub

    Private Sub oTx_estatura_TextChanged(sender As Object, e As EventArgs) Handles oTx_estatura.TextChanged
        oTx_imc.Text = CalcularIMC(Val(oTx_peso.Text), Val(oTx_estatura.Text))
    End Sub
End Class
